/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package appmanager

import (
	"errors"
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	"saas_sys_base/common"

	//"github.com/mjiulee/lego/utils"
)

/* 根据id获取记录
 * -----------------
 */
func (t *AppProduct) RecodeById(id int64) *AppProduct {
	item := new(AppProduct)
	_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}
	return item
}

func (t *AppProduct) RecodeByIdAndMiniAppId(id int64, miniAppId int64) *AppProduct {
	item := new(AppProduct)
	//_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	_, err := common.GetDBEngineWithFilterByMiniAppId(miniAppId).ID(id).Where("if_del = ?", 0).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}
	return item
}

/* 根据id获取记录
 * -----------------
 */
func (t *AppProduct) TenantRecodeById(id,tenantId int64) *AppProduct {
	item := new(AppProduct)
	_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).And("tenant_id = ?", tenantId).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}
	return item
}

/* 添加
 * -----------------
 */
func (t *AppProduct) AddRecode(item2add *AppProduct) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *AppProduct) AddRecodeWithsession(session *xorm.Session, item2add *AppProduct) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(硬删除)*/
func (t *AppProduct) DelRecodeById(id int64) bool {
	item := new(AppProduct)
	//_, err := lego.GetDBEngine().ID(id).Delete(item)
	item.IfDel = 1
	_, err := lego.GetDBEngine().ID(id).Cols("if_del").Update(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *AppProduct) DelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(AppProduct)
	//_, err := session.ID(id).Delete(item)
	item.IfDel = 1
	_, err := session.ID(id).Cols("if_del").Update(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 更新
 * -----------------*/
func (t *AppProduct) UpdateRecode(rc *AppProduct) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Omit("id", "if_del").MustCols("if_sale").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *AppProduct) UpdateRecodeWithSession(session *xorm.Session, rc *AppProduct) bool {
	_, err := session.ID(rc.Id).Omit("id", "if_del").MustCols("if_sale").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/*查询表名, 注意，函数名不能定义未TableName()，因为会与xorm内部使用的TableName冲突*/
func (t *AppProduct) TName() string {
	tname := lego.GetDBEngine().TableName(t)
	return tname
}


//
func (t *AppProduct) ListAppProductByWho(who *common.Who, page, psize int, keyword string, condiBean *AppProduct) ([]AppProduct, int64, error) {
	if page < 1 {
		return nil, 0, errors.New("para page start from 1")
	}

	if condiBean == nil {
		condiBean = new(AppProduct)
	}
	start := (page-1*psize)
	resultList := make([]AppProduct, 0)

	session := lego.GetDBEngine().Table(AppProduct{}).Alias("t1").
		//Join("INNER", []string{(AppProductSpec{}).TName(), "t2"}, "t1.id = t2.product_id").
		Where("t1.if_del = ?", 0).
		And("t1.mini_app_id = ?", who.MiniAppId).
		Limit(psize, start).
		Desc("t1.id")
	//OrderBy("asc")

	session2 := *session.Clone()
	err := session.Find(&resultList, condiBean)
	if err != nil {
		return nil, 0, err
	}

	total, err := session2.Count(condiBean)
	if err != nil {
		return nil, 0, err
	}
	return resultList, total, err
}
